using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using jajaja;
using PrimitivesSample;
using DIMP_Libraries;
using TetroxNetwork;

namespace tetrox_3D
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        gameManager myGame;

        List<netBlock> otherMap;
        netPiece otherPiece;

        ClientServeur myClient;

        public Game1(ClientServeur heySalut)
        {
            IsFixedTimeStep = false;
            this.myClient = heySalut;
            this.otherMap = new List<netBlock>();
            this.otherPiece = new netPiece();
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            heySalut.onMessageReceived += new EventHandler<MessageEventArg>(MessageRecu);
        }

        void MessageRecu(object sender, MessageEventArg e)
        {
            if (typeof(netMap) == e.Message.GetType())
            {
                this.otherMap = ((netMap)e.Message).Map;
            }
            if (typeof(netPiece) == e.Message.GetType())
            {
                this.otherPiece.Position = ((netPiece)e.Message).Position;
                this.otherPiece.Blocks = ((netPiece)e.Message).Blocks;
            }
            if (typeof(netAttaque) == e.Message.GetType())
            {
                this.myGame.Score += ((netAttaque)e.Message).Amplitude;
                this.myGame.Delay = (int)(1000 / (2 * (1 + ((float)this.myGame.Score / 30))));
                this.myGame.posScore = 5;
            }
            if (typeof(ActionMessage) == e.Message.GetType())
            {
                if (((ActionMessage)e.Message).Type == ActionType.Surrender)
                {
                    Console.WriteLine("ta gagner");
                    Exit();
                }
            }
        }

        protected override void Initialize()
        {
            TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 30);
            base.Initialize();
            camera myCamera = new camera(this.spriteBatch, new PrimitiveBatch(graphics.GraphicsDevice));
            myCamera.font = this.Content.Load<SpriteFont>("prout");
            myCamera.Position = new Vector3(215, 210, 401);
            myCamera.Orientation = new Vector2(2, (float)0.18);
            myCamera.FOV = 0.6;
            myCamera.TypeDessin = camera.dessin.full;
            this.myGame = new gameManager(myCamera, this.Content);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void Update(GameTime gameTime)
        {
             if(!this.myGame.Update(gameTime, this.myClient))
                 this.Surrender();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            this.spriteBatch.Begin();
            this.myGame.Draw();
            #region Draw otherMap

                this.myGame.Camera.DrawLine(new Vector2(0, 0), new Vector2(0, 42), Color.White);
                this.myGame.Camera.DrawLine(new Vector2(1, 0), new Vector2(1, 42), Color.White);
                this.myGame.Camera.DrawLine(new Vector2(23, 0), new Vector2(23, 42), Color.White);
                this.myGame.Camera.DrawLine(new Vector2(22, 0), new Vector2(22, 42), Color.White);
                this.myGame.Camera.DrawLine(new Vector2(1, 43), new Vector2(22, 43), Color.White);
                this.myGame.Camera.DrawLine(new Vector2(23, 42), new Vector2(0, 42), Color.White);
                foreach (var item in this.otherMap)
                {
                    this.myGame.Camera.DrawLine(
                        new Vector2(20 - item.Position.X * 2, 42 - item.Position.Y * 2),
                        new Vector2(20 - item.Position.X * 2, 42 - item.Position.Y * 2 + 2), item.Couleur);
                    this.myGame.Camera.DrawLine(
                        new Vector2(20 - item.Position.X * 2 + 1, 42 - item.Position.Y * 2),
                        new Vector2(20 - item.Position.X * 2 + 1, 42 - item.Position.Y * 2 + 2), item.Couleur);
                }
                foreach (var item in this.otherPiece.Blocks)
                {
                    this.myGame.Camera.DrawLine(
                        new Vector2(20 - (item.Position.X + this.otherPiece.Position.X) * 2, 42 - (this.otherPiece.Position.Y + item.Position.Y) * 2),
                        new Vector2(20 - (item.Position.X + this.otherPiece.Position.X) * 2, 42 + 2 - (this.otherPiece.Position.Y + item.Position.Y) * 2), item.Couleur);
                    this.myGame.Camera.DrawLine(
                        new Vector2(20 + 1 - (item.Position.X + this.otherPiece.Position.X) * 2, 42 - (this.otherPiece.Position.Y + item.Position.Y) * 2),
                        new Vector2(20 + 1 - (item.Position.X + this.otherPiece.Position.X) * 2, 42 + 2 - (this.otherPiece.Position.Y + item.Position.Y) * 2), item.Couleur);
                }
            #endregion
            this.spriteBatch.End();
            base.Draw(gameTime);
        }

        public void Surrender()
        {
            ActionMessage Lose = new ActionMessage();
            Lose.Type = ActionType.Surrender;
            this.myClient.SendMessage(Lose);
            Console.WriteLine("ta perdue gro naze");
            Exit();
            
        }
    }
}
